<template>
  <a-form-item :label="$t('cloudenv.text_14')" v-if="isPermission">
    <div class="d-flex align-items-center w-50">
      <a-select class="base-select" :loading="loading" showSearch :filterOption="filterOption" v-decorator="decorator">
        <a-select-option v-for="item of proxyOpts" :key="item.id" :value="item.id">
          {{item.name}} {{item.id === 'DIRECT' ? $t('cloudenv.text_110') :  null}}
        </a-select-option>
      </a-select>
      <a @click="fetchQueryProxy"><a-icon :spin="loading" type="sync" class="ml-2" /></a>
    </div>
    <div slot="extra">{{$t('cloudenv.text_111')}}<div>{{$t('cloudenv.text_112')}}<span class="link-color oc-pointer" @click="createProxySetting">{{$t('cloudenv.text_104')}}</span></div>
    </div>
  </a-form-item>
</template>

<script>
import { mapGetters } from 'vuex'
import { hasPermission } from '@/utils/auth'
import WindowsMixin from '@/mixins/windows'

export default {
  name: 'ProxySetting',
  mixins: [WindowsMixin],
  props: {
    account: {
      type: Object,
    },
    fc: {
      type: Object,
    },
    fd: {
      type: Object,
    },
  },
  data () {
    return {
      proxyOpts: [],
      loading: false,
    }
  },
  computed: {
    ...mapGetters(['isAdminMode', 'userInfo', 'l3PermissionEnable']),
    decorator () {
      return [
        'proxy_setting',
      ]
    },
    isPermission () {
      return hasPermission({ key: 'proxysettings_list' })
    },
  },
  created () {
    if (this.account && this.isPermission) {
      this.fetchQueryProxy()
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    async fetchQueryProxy () {
      const manager = new this.$Manager('proxysettings')
      if (this.loading) {
        return false
      }
      const params = {
        limit: 0,
      }
      if (this.account) {
        params.project_domain = this.account.domain_id
      } else if (this.l3PermissionEnable && this.isAdminMode) {
        params.project_domain = this.fc.getFieldValue('domain').key
      } else {
        params.project_domain = this.userInfo.projectDomainId
      }
      this.loading = true
      try {
        const { data } = await manager.list({ params })
        this.proxyOpts = data.data
        if (this.proxyOpts && this.proxyOpts.length > 0 && this.account) {
          this.fc.setFieldsValue({
            proxy_setting: this.account.proxy_setting_id,
          })
        } else {
          this.fc.setFieldsValue({
            proxy_setting: 'DIRECT',
          })
        }
      } catch (err) {
        throw err
      } finally {
        this.loading = false
      }
    },
    createProxySetting () {
      this.createDialog('ProxysettingCreateDialog', {
        domain: this.account ? this.account.domain_id : this.fd.domain || {},
        success: async () => {
          await this.fetchQueryProxy()
          if (this.proxyOpts && this.proxyOpts.length > 0) {
            this.fc.setFieldsValue({ proxy_setting: this.proxyOpts[0].id })
          }
        },
      })
    },
  },
}
</script>
